//
//  SignInViewController.swift
//  worldexchange
//
//  Created by xionghao on 2019/1/10.
//  Copyright © 2019 xionghao. All rights reserved.
//

import UIKit
import IQKeyboardManagerSwift

class SignInViewController: QMUICommonViewController {
    
    
    var loginComplete:(()->())?
    
    @IBOutlet weak var userWrapper: UIView!
    
    @IBOutlet weak var tf_password: UITextField!
    @IBOutlet weak var tf_username: UITextField!
    @IBOutlet weak var passwordWrapper: UIView!
    @IBOutlet weak var registerLabel: UILabel!
    @IBOutlet weak var btnSignIn: QMUIFillButton!
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "SignIn".localized()
        self.navigationItem.leftBarButtonItem = UIBarButtonItem.qmui_close(withTarget: self, action: #selector(closeClick))
        self.btnSignIn.setTitle(self.title, for: UIControl.State.normal)
        userWrapper.qmui_borderColor = UIColor.qmui_color(withHexString: "#d5d5d5")
        userWrapper.qmui_borderWidth = 0.5
        userWrapper.qmui_borderPosition = .bottom
        passwordWrapper.qmui_borderColor = UIColor.qmui_color(withHexString: "#d5d5d5")
        passwordWrapper.qmui_borderWidth = 0.5
        passwordWrapper.qmui_borderPosition = .bottom
        self.tf_username.placeholder = "InputUserName".localized()
         self.tf_password.placeholder = "InputPassword".localized()
        let tap = UITapGestureRecognizer(target: self, action: #selector(registerClick))
        self.registerLabel.addGestureRecognizer(tap)
        self.registerLabel.isUserInteractionEnabled = true
        self.registerLabel.text = "RegisterNow".localized()
        // Do any additional setup after loading the view.
    }
    
    @objc func registerClick() {
        let regVc = SignUpViewController()
        regVc.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(regVc, animated: true)
        
    }

    
    @objc func closeClick() {
        self.navigationController?.dismiss(animated: true, completion: {
            
        })
    }
    
    func login() {
       let loading =  QMUITips.showLoading("loading".localized(), in: self.navigationController!.view)
        provider.rx.request(WE.login(username: self.tf_username.text!, password: self.tf_password.text!)).filterSuccessfulStatusCodes().mapString()
            .subscribe(onSuccess: { (result) in
                if let loginResult = LoginReuslt.deserialize(from: result) {
                    if loginResult.code == 200 {
                        isLogin = true
                        userData = loginResult.data
                        QMUITips.hideAllTips()
                        QMUITips.showSucceed("LoginSuccess".localized())
                        NotificationCenter.default.post(name: NSNotification.Name.init(rawValue: "UserLoginChange"), object: nil)
                        self.closeClick()
                    } else {
                        QMUITips.hideAllTips()
                        QMUITips.show(withText: loginResult.message)
                    }
                }else {
                  QMUITips.hideAllTips()
                }
            }) { (error) in
                print(error)
                QMUITips.hideAllTips()
                 QMUITips.show(withText: "network error!")
        }
        
    }

    @IBAction func btnLoginClick(_ sender: QMUIFillButton) {
        if self.tf_username.text ==  "" {
            
            QMUITips.show(withText:  "usernameEmpty".localized())
            return
        }
        if self.tf_password.text ==  "" {
            QMUITips.show(withText: "passwordEmpty".localized())
            return
        }
        self.tf_password.endEditing(true)
         self.tf_username.endEditing(true)
        login()
    }
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
